import {componentRegistry, WORKFLOW_STYLE_DEFAULTS} from './ComponentRegistry'
import BaseNode from '../nodes/BaseNode.js'
import NodeDrawer from '../drawer/NodeDrawer.vue'
import StartDrawer from '../drawer/StartDrawer.vue'
import ApiDrawer from '../drawer/ApiDrawer.vue'
import CodeDrawer from '../drawer/CodeDrawer.vue'
import LLMDrawer from '../drawer/LLMDrawer.vue'
import TransitionDrawer from '../drawer/TransitionDrawer.vue'

/**
 * 组件注册配置
 * 用于注册所有工作流节点类型及其对应的配置抽屉组件
 */

// 注册开始节点
componentRegistry.registerNode(
    'start',
    {
        label: '开始',
        description: '流程开始节点',
        color: '#52c41a',
        iconFont: 'icon-kaishixunjian',
        iconUnicode: '\ue64d',
        defaultName: '开始',
        requiredFields: ['name', 'code'],
        defaultConfig: {
            inputs: [
                { key: 'order_id', label: 'order_id', type: 'String', required: true, builtIn: false }
            ]
        },
        width: 140,
        height: 40,
        ports: {
            items: [
                {id: 'right', group: 'right'}
            ],
            groups: {
                right: {
                    position: 'right',
                    attrs: {
                        circle: {...WORKFLOW_STYLE_DEFAULTS.portDot}
                    }
                }
            }
        }
    },
    BaseNode
)

// 注册循环开始节点
componentRegistry.registerNode(
    'loop-start',
    {
        label: '开始',
        description: '开始节点',
        color: '#52c41a',
        iconFont: 'icon-kaishixunjian',
        iconUnicode: '\ue64d',
        defaultName: '开始',
        hidden: true,
        width: 80,
        height: 40,
        ports: {
            items: [
                {id: 'right', group: 'right'}
            ],
            groups: {
                right: {
                    position: 'right',
                    attrs: {
                        circle: {...WORKFLOW_STYLE_DEFAULTS.portDot}
                    }
                }
            }
        }
    },
    BaseNode
)

// 注册 API 工具节点
componentRegistry.registerNode(
    'api',
    {
        label: 'API工具',
        description: '调用接口/HTTP请求',
        color: '#13c2c2',
        iconFont: 'icon-APIwangguan',
        iconUnicode: '\ue9eb',
        defaultName: 'API工具',
        requiredFields: ['name', 'code']
    },
    BaseNode
)

// 注册 代码 工具节点
componentRegistry.registerNode(
    'code',
    {
        label: '代码工具',
        description: '执行脚本/代码',
        color: '#722ed1',
        iconFont: 'icon-daima',
        iconUnicode: '\ue64f',
        defaultName: '代码工具',
        requiredFields: ['name', 'code']
    },
    BaseNode
)

// 注册 LLM 工具节点
componentRegistry.registerNode(
    'llm',
    {
        label: 'LLM',
        description: '大模型生成/推理',
        color: '#fa8c16',
        iconFont: 'icon-AIdamoxing',
        iconUnicode: '\ue885',
        defaultName: 'LLM',
        requiredFields: ['name', 'code']
    },
    BaseNode
)


// 注册 数据聚合 节点
componentRegistry.registerNode(
    'aggregation',
    {
        label: '数据聚合',
        description: '数据聚合',
        color: '#faad14',
        iconFont: 'icon-tiaojian',
        iconUnicode: '\ue6d8',
        defaultName: '数据聚合',
        requiredFields: ['name', 'code']
    },
    BaseNode
)

// 注册 循环（泳道风格）
componentRegistry.registerNode(
    'loop',
    {
      label: '循环',
      description: '循环（直接并发）',
      color: '#2871ce',
      fill: 'rgba(255,255,255,0.5)',
      iconFont: 'icon-santongshiguanli',
      iconUnicode: '\ue8cd',
      defaultName: '循环',
      requiredFields: ['name', 'code'],
      width: 450,
      height: 300,
      stroke: 'rgba(138,138,138,0.5)'
    },
    BaseNode
  )

// 注册结束节点
componentRegistry.registerNode(
    'end',
    {
        label: '结束',
        description: '流程结束节点',
        color: '#f5222d',
        iconFont: 'icon-zhongdian',
        iconUnicode: '\ue600',
        defaultName: '结束',
        requiredFields: ['name', 'code'],
        width: 140,
        height: 40,
        ports: {
            items: [
                {id: 'left', group: 'left'}
            ],
            groups: {
                left: {
                    position: 'left',
                    attrs: {
                        circle: {...WORKFLOW_STYLE_DEFAULTS.portDot}
                    }
                }
            }
        }
    },
    BaseNode
)

// 注册变迁边
componentRegistry.registerEdge(
    'transition',
    {
        label: '变迁',
        description: '流程变迁边',
        shape: 'edge',
        connector: 'smooth'
    }
)

// 注册 插件 通用节点
componentRegistry.registerNode(
    'plugin',
    {
        label: '插件',
        description: '通用插件节点',
        color: '#7184a8',
        iconUnicode: '\ue64f',
        defaultName: '插件',
        hidden: true
    },
    BaseNode
)

// 预置插件（后续将从后台配置接口获取）
componentRegistry.registerTool('timer', {
    label: '定时器',
    description: '按间隔触发',
    color: '#2f54eb'
})
componentRegistry.registerTool('webhook', {
    label: 'Webhook',
    description: '回调/通知',
    color: '#13c2c2'
})
componentRegistry.registerTool('storage', {
    label: '存储',
    description: '持久化数据',
    color: '#faad14'
})
componentRegistry.registerTool('logger', {
    label: '日志',
    description: '记录运行日志',
    color: '#722ed1'
})
componentRegistry.registerTool('notifier', {
    label: '通知',
    description: '邮件/短信/站内信',
    color: '#f5222d'
})

// 抽屉组件注册映射
componentRegistry.registerDrawer('start', StartDrawer)
componentRegistry.registerDrawer('api', ApiDrawer)
componentRegistry.registerDrawer('code', CodeDrawer)
componentRegistry.registerDrawer('llm', LLMDrawer)
componentRegistry.registerDrawer('aggregation', NodeDrawer)
componentRegistry.registerDrawer('loop', NodeDrawer)
componentRegistry.registerDrawer('end', NodeDrawer)
componentRegistry.registerDrawer('transition', TransitionDrawer)
